package com.olap.starter.util.sql;


import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.*;
import net.sf.jsqlparser.util.TablesNamesFinder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TableExtractor {

    public static void main(String[] args) throws JSQLParserException {

        String sql2 = "select t1.uuids,t1.uuid AS t1_uuid,t1.version AS t1_version,t1.update_time AS t1_update_time,t1.create_time AS t1_create_time,t1.deleted AS t1_deleted,t1.title AS t1_title,t1.width AS t1_width,t1.designer AS t1_designer,t1.theme AS t1_theme,t2.update_user AS t2_update_user,t2.create_time AS t2_create_time,t2.update_time AS t2_update_time,t2.remark AS t2_remark,t3.list_class AS t3_list_class,t3.is_default AS t3_is_default,t3.status AS t3_status,t3.create_user AS t3_create_user,t3.update_user AS t3_update_user,t3.create_time AS t3_create_time,t3.update_time AS t3_update_time,t3.remark AS t3_remark from form_main t1   join sys_dict_type t2 on t1.id=t2.parid  join sys_dict_data t3 on t2.id=t3.parid";
        String sql = "select t1.id AS t1_id,t1.form_name AS t1_form_name,t1.back_end AS t1_back_end,t1.table_name AS t1_table_name,t1.class_name AS t1_class_name,t1.uuid AS t1_uuid,t1.version AS t1_version,t1.update_time AS t1_update_time,t1.create_time AS t1_create_time,t1.deleted AS t1_deleted,t1.title AS t1_title,t1.width AS t1_width,t1.designer AS t1_designer,t1.theme AS t1_theme,t2.code AS t2_code,t2.name AS t2_name,t2.description AS t2_description,t2.status AS t2_status,t2.create_user AS t2_create_user,t2.update_user AS t2_update_user,t2.create_time AS t2_create_time,t2.update_time AS t2_update_time,t2.remark AS t2_remark from " +
                "form_main t1  " +
                "join sys_dict_type t2 on t1.id=t2.parid";
        // 根据sql创建select
        Select stmt = (Select) CCJSqlParserUtil.parse(sql2);
        TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
        List<String> tableList = tablesNamesFinder.getTableList(stmt);
        System.out.println(tableList);
        Map<String, Object> map = new HashMap<>();
        Map<String, String> mapTable = new HashMap<>();
        PlainSelect selectBody = (PlainSelect) stmt.getSelectBody();
        if (selectBody.getFromItem() != null) {
            selectBody.getFromItem().accept(new FromItemVisitorAdapter() {
                @Override
                public void visit(Table table) {
                    String asName = null;
                    System.out.println("table.getName()："+table.getName());
                    if(table.getAlias() != null){
                        asName = table.getAlias().getName();
                        // 获取别名 => 表名
                    }
                    mapTable.put( table.getName(),asName);

                }
            });
        }
        if (selectBody.getWhere() != null) {
            selectBody.getWhere().accept(new ExpressionVisitorAdapter() {
                @Override
                public void visit(AndExpression expr) {
                    // 获取where表达式
                    System.out.println(expr);
                }
            });
        }

        if (selectBody.getSelectItems() != null) {
            for (SelectItem selectItem : selectBody.getSelectItems()) {
                selectItem.accept(new SelectItemVisitorAdapter() {
                    @Override
                    public void visit(SelectExpressionItem item) {
                        String asName = null;
                        if(item.getAlias() != null){
                            // 获取字段别名 => 字段名
                            asName = item.getAlias().getName();
                        }
                        map.put(item.getExpression().toString(), asName);
                    }
                });
            }
        }


        System.out.println("map " + map);
        System.out.println("mapTables" + mapTable);
    }
}
